suppressPackageStartupMessages({
library(leaflet)
library(sf)
library(sp)
library(dplyr)
library(rgdal)
})

1 Carga de Mapas Interactivos

CCAA_MAP<-readOGR("C:/Users/Laura/Documents/Mineria_3/2.Espacial/Estadistica Espacial en R/cartografias","CCAA_SALARIOS")
## OGR data source with driver: ESRI Shapefile 
## Source: "C:\Users\Laura\Documents\Mineria_3\2.Espacial\Estadistica Espacial en R\cartografias", layer: "CCAA_SALARIOS"
## with 19 features
## It has 5 fields
summary(CCAA_MAP)
## Object of class SpatialPolygonsDataFrame
## Coordinates:
##         min       max
## x -12.64805  4.329524
## y  34.21532 43.791900
## Is projected: FALSE 
## proj4string :
## [+proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs]
## Data attributes:
##     SP_ID                id              cd_CCAA            Nm_CCAA         
##  Length:19          Length:19          Length:19          Length:19         
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##                                                                             
##                                                                             
##                                                                             
##                                                                             
##     SALARIO     
##  Min.   :11.54  
##  1st Qu.:12.53  
##  Median :12.83  
##  Mean   :13.57  
##  3rd Qu.:14.24  
##  Max.   :18.72  
##  NA's   :2

2 ¿Que hace la libreria leaflet?

leaflet(CCAA_MAP,options = leafletOptions(attributionControl = FALSE)) %>%
  addPolygons(data=CCAA_MAP, stroke=TRUE, opacity = 0.5, fillOpacity = 0.7,color="grey10",
              fillColor = ~colorQuantile("YlOrBr", n=9, SALARIO, na.color = "white")(SALARIO))
leaflet(CCAA_MAP,options = leafletOptions(attributionControl = FALSE)) %>%
  addTiles()%>%
  addPolygons(data=CCAA_MAP, stroke=TRUE, color="grey10")
leaflet(CCAA_MAP,options = leafletOptions(attributionControl = FALSE)) %>%
  addTiles()%>%
  addPolygons(data=CCAA_MAP, stroke=TRUE, opacity = 0.25, fillOpacity = 0.27,color="grey10",
              fillColor = ~colorQuantile("YlOrBr", n=9, SALARIO, na.color = "white")(SALARIO))

Las anteriores imagenes representan la distribución de salarios por provincia.

#Librerias para hacerlo interactivo
suppressPackageStartupMessages({
library(tmaptools)
library(tmap)
})
## Warning: package 'tmaptools' was built under R version 4.1.3
## Warning: package 'tmap' was built under R version 4.1.3

3 Mapas Interactivos

tm_shape(CCAA_MAP) +
  tm_fill(palette ="Blues",col = "SALARIO",style = "quantile")


4 Situar Marcadores con leaflet

datos_map<-data.frame(longx=c(-3.741274,-3.718765,-3.707027,-3.737117, -3.674605,-3.709559 ),
                      laty=c(40.38479, 40.36751, 40.45495,40.44672, 40.50615, 40.42059))

awesome <- makeAwesomeIcon(
  icon = "circle",
  iconColor = "White",
  markerColor = "blue",
  library = "fa"
)

m <-leaflet(datos_map) %>%
  addTiles()%>%  
  addAwesomeMarkers(~longx,~laty,  popup = ~paste(laty, longx, sep = '<br/>'), icon = awesome)

m 
objetivo<-"Facultad de Estudios Estadísticos, Madrid"
geo_output<-geocode_OSM(objetivo, details=TRUE, return.first.only = TRUE, as.data.frame = T )

laty=geo_output$lat
lonx=geo_output$lon



awesome <- makeAwesomeIcon(
  icon = "circle",
  iconColor = "White",
  markerColor = "blue",
  library = "fa"
)

leaflet(data.frame(lonx,laty)) %>% 
  addTiles() %>% 
  addAwesomeMarkers(lng=~lonx, lat=~laty,icon = awesome)